/**
 * 详情对话框
 */
var BasicAddressInfoDlg = {
    data: {
        address: "",
		longitude:"",
		latitude:"",
    }
};
layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 地址修改申请表管理
     */
    var TransportAddressEdit = {
        tableId: "transportAddressEditTable"
    };

    /**
     * 初始化表格的列
     */
    TransportAddressEdit.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 160},
            {field: 'state', sort: true, title: '状态', style:'font-weight: 600;',
            	templet: function(d){
            		var str = "";
            		if (d.state == '已处理'){
                    	str += '<span style="color:#19be6b;font-weight: bold;">' + d.state + '</span>'
					} else {
                    	str += '<span style="font-weight: bold;">' + d.state + '</span>'
					}
            		return str
            	}
			},
            {field: 'dispatchNumber', sort: true, title: '调度单号', style:'font-weight: 600;',templet: '#dispatchNumberTpl'},
            {field: 'getUnit', sort: true, title: '收货客户'},
            {field: 'getLocation', sort: true, title: '地址'},
            {field: 'longitude', sort: true, title: '经度'},
            {field: 'latitude', sort: true, title: '维度'},
            {field: 'carnumber', sort: true, title: '车牌号'},
            {field: 'createBy', sort: true, title: '申请人'},
            {field: 'createTime', sort: true, title: '申请时间'},
            {field: 'examineName', sort: true, title: '审核人'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    TransportAddressEdit.search = function () {
        var queryData = {};
        queryData['state'] = $("#state").val();
        queryData['condition'] = $("#condition").val();
        table.reload(TransportAddressEdit.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

     // 批量审核
     $('#btnSubmission').click(function () {
    	var checkStatus = table.checkStatus('transportAddressEditTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
			
            layer.confirm('确定通过审核吗?', function(index){
            	layer.close(index);
            	var msg;
                $.ajax({
                	url:Feng.ctxPath + "/transportAddressEdit/tongguo?ids="+ids,
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                		msg = layer.msg('提交中。。。请耐心等待', {
                			  icon: 16,
                			  shade: [0.2,'#000'], //0.1透明度的白色背景
                			  time: false
                		});
                	},
                	success:function(data){
                    	layer.close(msg); 
                		if (data.success) {
			            	Feng.success("提交成功!");
            				table.reload(TransportAddressEdit.tableId);
						} else {
  		                    Feng.error("提交失败！"+data.message);
						}
                	},
                	error:function(data){
                    	layer.close(msg); 
                        Feng.error("提交失败！" + data.responseJSON.message)
                	}
                })
            }); 
		} else {
			layer.alert('请至少选中一条数据');
		}
     });

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TransportAddressEdit.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/transportAddressEdit/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TransportAddressEdit.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TransportAddressEdit.tableId,
        url: Feng.ctxPath + '/transportAddressEdit/list',
        page: true,
        limit: 30,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportAddressEdit.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TransportAddressEdit.search();
    });

    // 工具条点击事件
    table.on('tool(' + TransportAddressEdit.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
	        layer.open({
	            type: 2,
	            title: '选择地址',
	            area: ['800px', '550px'],
	            content: Feng.ctxPath + '/basicAddress/toChoiceAddress?address=' + data.getLocation +"&longitude=" + data.longitude +"&latitude=" + data.latitude, 
	            end: function () {
					if(BasicAddressInfoDlg.data.address != null && BasicAddressInfoDlg.data.address != ''){
				        var ajax = new $ax(Feng.ctxPath + "/transportAddressEdit/editItem?id="+data.id+"&getLocation="+BasicAddressInfoDlg.data.address+"&longitude="+BasicAddressInfoDlg.data.longitude+"&latitude="+BasicAddressInfoDlg.data.latitude, function (data) {
				    		if (data.success) {
				                Feng.success("修改成功!");
                				table.reload(TransportAddressEdit.tableId);
							} else {
				                Feng.error("修改失败！"+data.message);
							}
				        }, function (data) {
				            Feng.error("修改失败!" + data.responseJSON.message + "!");
				        });
				        ajax.start();
					}
	            }
	        });
        } else if (layEvent === 'delete') {
            TransportAddressEdit.onDeleteItem(data);
        } else if (layEvent === 'tooDetailed') {
            layer.open({
                type: 2,
                title: '配送派车信息',
                area: ['1250px', '570px'],
                anim: 5,
                content: Feng.ctxPath + '/tDispatch/tooDetailed2?dispatchNumber=' + data.dispatchNumber,
            });
        }
    });
});
